﻿#include "Logic.h"
#include "DataRepository.h"
#include "GnsClientFrame.h"
#include "HelloWorldScene.h"

#include "AppDelegate.h"
#include "GNS_LoginScene.h"
#include "GNS_HallScene.h"

USING_NS_CC;

// 建立客户端框架系统
void createClientFrame()
{
	// 创建事件桥梁
	IEventBridge* pIEventBridge = IEventBridge::getInstance();
	assert(pIEventBridge);

	// 创建逻辑模块
	ILogic* pLogic = CLogic::getInstance();
	assert(pLogic);
	pLogic->setCircleParam(1,															// OnConnect 状态下开始转圈的连接次数
		RETRY_CONNECT_MAX_COUNT / 2,		// OnConnect 状态下结束转圈的连接次数
		5,															// OnReconnectAfterOffline 状态下开始转圈的连接次数
		RETRY_CONNECT_MAX_COUNT/2			// OnReconnectAfterOffline 状态下结束转圈的连接次数
		);

	// 创建数据区
	IDataRepository* pDataRepository = CDataRepository::getInstance();
	assert(pDataRepository);

	//--- 创建框架 ----------------------------
	GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
	assert(pGnsClientFrame);

	{
		// 设置内存块的参数
		pGnsClientFrame->setMemBlock(CMemBlockPool2::DEF_MEMBLOCK_SIZE, CMemBlockPool2::DEF_INIT_COUNT, CMemBlockPool2::DEF_MAX_COUNT);

		// 设置逻辑模块
		pGnsClientFrame->setLogic(pLogic);

		// 设置数据区模块
		pGnsClientFrame->setDataRepository(pDataRepository);
	}

	int iClientId = 0;
	if (1)
	{	// 第 0 个客户端
#if 1
		//ClientSocket* pClient = pGnsClientFrame->registClient(iClientId++, "127.0.0.1", 8700);
		ClientSocket* pClient = pGnsClientFrame->registClient(iClientId++, "112.74.194.72", 8700);
#else
	    ClientSocket* pClient = pGnsClientFrame->registClient(iClientId++, "192.168.1.97", 8700);
#endif

		assert(pClient);
		if (pClient)
		{
			pClient->setReconnectInfo(800, 30);
		}
	}

	if (0)
	{	// 第 1 个客户端
		ClientSocket* pClient = pGnsClientFrame->registClient(iClientId++, "127.0.0.1", 5180);
		assert(pClient);
		if (pClient)
		{
			pClient->setReconnectInfo(800, 30);
		}
	}

	pGnsClientFrame->init();
	pGnsClientFrame->start();

	// int iTmp = 0;
}

// 销毁客户端框架系统
void destroyClientFrame()
{
	// 释放框架
	GnsClientFrame::destoryInstance();

	// 销毁数据区
	CDataRepository::destoryInstance();

	// 销毁逻辑模块
	CLogic::destoryInstance();

	// 销毁事件桥梁
	IEventBridge::destoryInstance();

	// int iTmp = 0;
}

AppDelegate::AppDelegate()
{
	createClientFrame();		// 建立客户端框架系统
}

AppDelegate::~AppDelegate()
{
	destroyClientFrame();		// 销毁客户端框架系统
}

//if you want a different context,just modify the value of glContextAttrs
//it will takes effect on all platforms
void AppDelegate::initGLContextAttrs()
{
	//set OpenGL context attributions,now can only set six attributions:
	//red,green,blue,alpha,depth,stencil
	GLContextAttrs glContextAttrs = { 8, 8, 8, 8, 24, 8 };

	GLView::setGLContextAttrs(glContextAttrs);
}

bool AppDelegate::applicationDidFinishLaunching() {
	// initialize director
	auto director = Director::getInstance();
	auto glview = director->getOpenGLView();
	if (!glview) {
		glview = GLViewImpl::createWithRect("MobileManage", Rect(0, 0, 640, 960));
		director->setOpenGLView(glview);
	}

	director->getOpenGLView()->setDesignResolutionSize(640, 1136, ResolutionPolicy::EXACT_FIT);

	// turn on display FPS
	director->setDisplayStats(true);

	// set FPS. the default value is 1.0/60 if you don't call this
	director->setAnimationInterval(1.0 / 60);

	FileUtils::getInstance()->addSearchPath("res");   //搜索直接路径

	// create a scene. it's an autorelease object
#if 1
	auto scene = GNS_LoginScene::createScene();
#else
	auto scene = HelloWorld::createScene();
#endif

	// run
	director->runWithScene(scene);

	return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
	Director::getInstance()->stopAnimation();
	//    Director::getInstance()->end();
	abort();
	// if you use SimpleAudioEngine, it must be pause
	// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
	Director::getInstance()->startAnimation();

	// if you use SimpleAudioEngine, it must resume here
	// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
